<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
      center
    >
      <el-card class="box-card" style="margin-bottom: 20px">
        <div slot="header" class="clearfix">
          <span>{{ $t("dedicated_contribution_plan") }}</span>
        </div>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              >{{ $t("add") }}</el-button
            >
          </el-col>
        </el-row>
        <el-table :data="formList" border :max-height="tableHeight">
          <el-table-column
            :label="$t('employer_name')"
            align="center"
            prop="employerName"
          >
          </el-table-column>
          <el-table-column
            :label="$t('plan_name')"
            align="center"
            prop="schemeName"
          >
          </el-table-column>
          <el-table-column
            :label="$t('plan_type')"
            align="center"
            prop="schemeType"
          >
            <template slot-scope="{ row }">
              {{ dict.label.contribution_plan_type[row.schemeType] }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('action')" align="center" width="100">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleEdit(scope.row, scope.$index)"
                >{{ $t("modify") }}</el-button
              >
              <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.$index)"
                >{{ $t("delete") }}</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{
          $t("confirm")
        }}</el-button>
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>
    <employeePlan
      ref="employeePlan"
      @editPlan="editPlan"
      @addPlan="addPlan"
    ></employeePlan>
  </div>
</template>
<script>
import {
  getPlan,
  updatePlan,
  employeeSpecialContribution,
} from "@/api/custom/employeeList";
import employeePlan from "./plan.vue";
export default {
  dicts: ["contribution_plan_type", "yes_or_no"],
  components: {
    employeePlan,
  },
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      formList: [],
    };
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.formList = [];
      this.resetForm("form");
    },
    handleEdit(item, index) {
      if(item.id){
        employeeSpecialContribution(item.id).then((res) => {
          this.$refs.employeePlan.handlePlan(res.data, index);
          this.$refs.employeePlan.title = this.$t("modify");
        });
      }else{
        this.$refs.employeePlan.handlePlan(item, index);
          this.$refs.employeePlan.title = this.$t("modify");
      }
      
    },
    /** 新增按钮操作 */
    handlePlan(row) {
      this.reset();
      this.form = row;
      getPlan(row.id).then((res) => {
        if (res.data) {
          this.formList = res.data;
        }
        this.open = true;
        this.title = this.$t("dedicated_contribution_plan");
      });
    },
    editPlan(data, index) {
      this.$set(this.formList, index, data);
    },
    addPlan(data) {
      this.formList.push(data);
    },
    /** 提交按钮 */
    submitForm() {
      if (!this.formList.length) {
        this.$message.warning(
          this.$t("please_select", {
            name: this.$t("dedicated_contribution_plan"),
          })
        );
        return;
      }
      updatePlan(this.formList).then((response) => {
        this.$modal.msgSuccess(this.$t("operation_successful"));
        this.open = false;
        this.$emit("refreshList");
      });
    },
    handleAdd() {
      this.$refs.employeePlan.handlePlan({
        empId: this.form.id,
      });
      this.$refs.employeePlan.title = this.$t("add_new_plan");
    },
    handleDelete(index) {
      this.formList.splice(index, 1);
    },
    getStatusText(row) {
      if (row.dateExpiration && row.dateDffective) {
        const newDataTime = new Date().getTime();
        const endDateTime = new Date(row.dateExpiration).getTime();
        const startDateTime = new Date(row.dateDffective).getTime();
        if (startDateTime > newDataTime) {
          return this.$t("not_effective");
        } else if (endDateTime > newDataTime) {
          return this.$t("in_effect");
        } else {
          return this.$t("expired");
        }
      } else {
        return "";
      }
    },
  },
};
</script>
